package alo.p11.work;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FirstRepetition
 * @Description 第一个将会重复的元素
 * @Author Fanxc
 * @DATE 2021/3/12 19:31
 * @Version 1.0
 */
public class FirstRepetition {
    int firstRepetition(int[] A){
        HashMap<Integer,Integer> hs=new HashMap<>();
        for (int i = 0; i < A.length; i++) {
            if (!hs.containsKey(A[i])){
                hs.put(A[i],i);
            }else {
                hs.put(A[i],-Math.abs(hs.get(A[i])));
            }
        }
        int max=-30;
        int key=-1;
        for (Map.Entry entry:hs.entrySet()) {
            if ((int)entry.getValue()<0){
                if((int)entry.getValue()>max){
                    max = (int)entry.getValue();
                    key = (int)entry.getKey();
                }
            }
        }
        return key;
    }
}
